from os import path, getcwd
from tkinter import PhotoImage, messagebox, HORIZONTAL ,ttk, filedialog, Tk, StringVar, Button, Canvas, Frame, Label, Toplevel
from datetime import datetime
from My_utils import job
import time


class UI(Frame):
    def __init__(self):
        self.__task = 0 
        self.__taskNum = 0
        self.window = Tk()
        self.window.iconbitmap(f'{path.dirname(path.abspath(__file__))}\\Icons\\icon.ico')
        self.__photo = PhotoImage(file=f'{path.dirname(path.abspath(__file__))}\\Icons\\flag.png')
        self.__imgLabel = Label(self.window, image=self.__photo)
        self.__compare_button_text = StringVar()
        self.__compare_button_text.set("结果反馈自动核查")
        self.__screenwidth = self.window.winfo_screenwidth()
        self.__screenheight = self.window.winfo_screenheight()
        self.__width = 400
        self.__height = 280
        self.__size = '%dx%d+%d+%d' % (self.__width, self.__height, (self.__screenwidth -self.__width)/2, (self.__screenheight - self.__height)/2)
        self.__compare_button = Button(self.window, width=16, height=2, textvariable=self.__compare_button_text, font=(
            'microsoft yahei', 9), command=self.__compare_main)

    def place_element(self):
        self.window.title('集中调度平台结果反馈核查工具V1')
        self.__imgLabel.place(x=120,y=80)
        self.__compare_button.place(x=140, y=180)
        self.window.geometry(self.__size)
        self.window.maxsize(self.__width, self.__height)
    
    
    def __compare_main(self):
        try:
            self.__compare_button_text.set("正在核查中")
            messagebox.showinfo(title='提示', message='正在核查，界面可能会卡住，请耐心等待')
            if job():
                messagebox.showinfo(title='提示', message='已完成,请检查邮件')
                self.__compare_button_text.set("结果反馈自动核查")
            else:
                messagebox.showerror(title='错误', message='出错请重试')
                self.__compare_button_text.set("结果反馈自动核查")
        except Exception as e:
            messagebox.showerror(title='错误', message='失败请重试')
            self.__compare_button_text.set("结果反馈自动核查")



    def on_closing(self):
        if messagebox.askokcancel("退出", "程序正在执行中，确定退出？"):
            self.window.destroy()
    
if __name__ == '__main__':
    window1 = UI()
    window1.place_element()
    window1.window.protocol("WM_DELETE_WINDOW", window1.on_closing)
    window1.window.mainloop()